/**
 * 数据邮件
 * @authors supmain (mtingfeng@gmail.com)
 * @date    2015-01-20 09:34:53
 * @for 数据字典.html
 * @version $1.0$
 */
var BSTMG = BSTMG || {};
BSTMG.emailmanager = {
	init: function(){
		var that = this;
		//邮件列表
		that.showEmailList();
		
		that.initSelectors();
		/* 表格全选 */
		that.tableCheckAll();
		/* 邮件操作 */
		that.emailManager();
	},
	showEmailList: function(){
		$.ajax({
				url: "/sysAdmin/email/emailList",
				type: 'post',
				dataType: 'json',
				success: function(data){
					var emailList = data.emailList;
					var $tbody = $("#emailList tbody"),
		            	html = '';
			        if (emailList.length > 0) {
			            for (var i = 0; i < emailList.length; i++) {
			                var userInfo = emailList[i];
			                html += '<tr>';
			                html += '<td><input type="checkbox" class="simple" data-id="'+userInfo.emailId+'"></td>';
			                html += '<td>' + BSTMG.tools.checkNull(userInfo.emailSubject) + '</td>';
			                html += '<td>' + BSTMG.tools.checkNull(userInfo.emailTypeName) + '</td>';
			                html += '<td>' + BSTMG.tools.checkNull(userInfo.fromAddress) + '</td>';
			                html += '<td class="opration">';
			                html += '<a class="btn btn-default look" href="/sysAdmin/email/emailDetail/'+userInfo.emailId+'">查看</a>';
			                html += '<a class="btn btn-default edit" href="/sysAdmin/email/updateEmail/'+userInfo.emailId+'">编辑</a> ';
			                //html += '<a class="btn btn-default edit">编辑</a> ';
	                		html += '<a class="btn btn-default delete">删除</a>';
			                html += '</td></tr>';
			            }
			            $tbody.html(html);
			        } else {
			            var $tr = '<tr class="nodata"><td colspan="5">暂无数据！</td></tr>';
			            $tbody.html($tr);
			        }
				},
				error: function(error){
					BSTYUN.dialog.warn("网络异常，请联系管理员！");
				}
			});
	},
	initSelectors: function(){
		var that = this;
		that["query"] = {};
		that["query"]["#sysEmailManager"] = $("#sysEmailManager");
		// 操作
		that["query"][".operators"] = that["query"]["#sysEmailManager"].find(".operators");
		// 表格
		that["query"][".bstable"] = that["query"]["#sysEmailManager"].find(".bstable");
	},
	tableCheckAll: function(){
		var that = this,
			$table = that["query"][".bstable"],
			$thead = $table.find("thead"),
			$tbody = $table.find("tbody");
		// 全选
		$thead.on("click",".simple",function(){
			var $this = $(this),
				$parent = $this.parents('table.table'),
				$checkbox = $parent.find('input[type=checkbox]').not($this);
			$this.prop("checked") ? $checkbox.prop("checked",true) : $checkbox.prop("checked",false) ;
			// 改变对应操作的可用性
			changeIsOperatot( $checkbox );
		});
		$tbody.on("click","input[type=checkbox]",function(){
			var $this = $(this),
				$all = $this.parents("tbody").eq(0).find("input[type=checkbox]"),
				$checkbox = $all.not($this).filter(":checked");
			var $checkall = $this.parents('table.table').find("thead input.simple");
			( $this.prop("checked") && ($checkbox.length + 1) == $all.length ) ? $checkall.prop("checked",true) : $checkall.prop("checked",false) ;
			
			// 改变对应操作的可用性
			changeIsOperatot( $all );
		});
		function changeIsOperatot( $all ){
			var $op_check = $all.filter(":checked"),
				length = $op_check.length,
				$operator = $all.eq(0).parents(".bstable").prev(".operators"),
				$btn = $operator.find(".btn");
			$btn.removeClass("disabled");
			if( length == 0 ){
				$btn.not(".addnew").addClass("disabled");
			}else if( length == 1 ){
				$operator.find(".addsub,.edit").removeClass("disabled");
			}else{
				$operator.find(".addsub,.edit").addClass("disabled");
			};
		};
	},
	emailManager: function(){
		var that = this,
			$operators = that["query"][".operators"],
			$table = that["query"][".bstable"].eq(0);
		$operators.on("click",".delete",function(){
			var $checkbox = checkedCategory();
			if( $checkbox.length == 0 ){
				BSTYUN.dialog.warn("请选择一条记录！");
				return false;
			};
			deleteCategory( $checkbox );
		});
		$table.on("click",".look",function(){
			
		}).on("click",".edit",function(){
			
		}).on("click",".delete",function(){
			var $checked = $(this).parent().parent().find("td:first-child input");
			deleteCategory( $checked );
		});
		// 查找选中的id
		function checkedCategory(){
			return  $table.find("tbody input.simple").filter(":checked");
		};
		// 新增
		function addNew( callback ){
			
		};
		// 新增子类
		function addSub( $checkbox ){
			
		};
		// 编辑分类
		function editCategory( $checkbox ){
			
		};
		// 删除
		function deleteCategory( $checkbox ){
			var ids = [];
			$checkbox.each(function(index,val){
				ids.push( $.trim( $(this).attr("data-id") ) );
			});
			BSTYUN.dialog.confirm("确定要删除选中项吗？",function(){
				$.ajax({
	 				url: '/sysAdmin/operate/email/deleteSysEmail',
	 				type: 'post',
	 				dataType: 'json',
	 				data: { ids: ids.join(",")} ,
	 				beforeSend: function(){},
	 				complete: function(){},
	 				success: function(data){
	 					// 删除服务器成功删除的项目
	 					$checkbox.each(function(index,val){
	 						var $this = $(this);
	 						$this.parent().parent().remove();
	 					});
	 				},
	 				error: function(error){
	 					BSTYUN.dialog.warn("网络异常，请联系管理员！");
	 				}
	 			});
			});
		};
	}
};

$(function(){
	BSTMG.emailmanager.init();
});